$(function () {
    console.log('dom准备好了')

    $('#btnQuery').click(function () {
        let keyword = $('[name=keyword]').val();
        let obj = {
            keyword
        }
        $.post('/Home/Query', obj, function (res) {
            let tb = $('#tb');
            let tbtr = $('.tbtr');
            tbtr.html("");
            res.forEach(item => {
                let html = `
                        <tr class="tbtr">
                        <td>${item.id}</td>
                        <td>${item.productName}</td>
                        <td>${item.supplier}</td>
                        <td>${item.price}</td>
                        <td>${item.stock}</td>
                        <td>${item.remarks}</td>
                        <td>
                            <input type="button" onclick="btnEdit(${item.id})" value="编辑">
                            <input type="button" onclick="btnDel(${item.id})" value="删除">
                        </td>
                    </tr>
                `
                tb.append(html);
            });
        })
    })
    $('#btnAdd').click(function () {
        window.location.href = '/Home/CreateOrEdit'
    })
    $('#btnSave').click(function () {
        let Id = $('[name=Id]').val();
        let ProductName = $('[name=ProductName]').val();
        let Supplier = $('[name=Supplier]').val();
        let Price = $('[name=Price]').val();
        let Stock = $('[name=Stock]').val();
        let Remarks = $('[name=Remarks]').val();
        let obj = {
            Id, ProductName, Supplier, Price, Stock, Stock,Remarks
        }
        $.post('/Home/Save', obj, function () {
            alert('保存成功')
            window.location.href = '/Home/Index'
        })
    })
    $('#btnCencel').click(function () {
        window.location.href = '/Home/Index'

    })
})
function btnEdit(id) {
    window.location.href = `/Home/CreateOrEdit/${id}`

}
function btnDel(id) {
    if (confirm('确定要删除吗')) {
        window.location.href = `/Home/Delete/${id}`
    }
}